<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2019-06-14
 * Time: 12:16
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1);

error_reporting(0);
require_once '/data/wwwroot/g_vendor/autoload.php';


$orderList = \Biz\model\trade\Order::select("*", ["express_company[!]" => "", "express_no[!]" => "","is_package_delivery"=>0]);

if (empty($orderList)) {
    return;
}

foreach ($orderList as $orderInfo) {

    //查询order_package表是否有数据
    $orderPackage = \Biz\model\trade\OrderPackage::getFirstPackageInfo($orderInfo['order_no']);

    //order_package表有数据，说明已转换
    if (!empty($orderPackage)) {
        continue;
    }

    //orderpackage表没有数据那么则进行转换

    $skuIdList = \Biz\model\trade\OrderGoods::getSkuIds($orderInfo['order_no']);
    if (empty($skuIdList)) {
        continue;
    }


    $orderLogisticslog = \Biz\model\trade\OrderLogistics::get("*", ["order_no" => $orderInfo['order_no'], "package_id" => 0]);

    if (empty($orderLogisticslog)) {
        continue;
    }

    if (!empty($orderInfo['delivery_time'])){
        //创建包裹，并进行转换
        $skuIdStrings = trim(implode(",", $skuIdList), ",");

        $packageId = \Biz\model\trade\OrderPackage::createPackage($orderInfo['uid'], $orderInfo['order_no'], $skuIdStrings, $orderInfo['express_company'], $orderInfo['express_no'],$orderInfo['delivery_time']);

        $result = \Biz\model\trade\OrderLogistics::update(["package_id" => $packageId], ["order_no" => $orderInfo['order_no'], "package_id" => 0]);

        if (!empty($result) && is_object($result) && method_exists($result, "rowCount")) {
            if ($result->rowCount() > 0) {

                \Biz\model\trade\Order::update(["is_package_delivery"=>1],["order_no"=>$orderInfo['order_no']]);

                echo "order_no=" . $orderInfo['order_no'] . "   package_id=" . $packageId . " status=1\n";
                continue;
            }
        }
    }
    echo "order_no=" . $orderInfo['order_no'] . "   package_id=" . $packageId . " status=0\n";

}